package com.bangbang.service.impl;

import com.bangbang.exception.SeckillExcrption;
import com.bangbang.pojo.TbSeckillCourse;
import com.bangbang.mapper.TbSeckillCourseMapper;
import com.bangbang.service.ITbSeckillCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 秒杀课程商品信息表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-08-12
 */
@Service
public class TbSeckillCourseServiceImpl extends ServiceImpl<TbSeckillCourseMapper, TbSeckillCourse> implements ITbSeckillCourseService {

    @Autowired
    private TbSeckillCourseMapper tbSeckillCourseMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public List<TbSeckillCourse> selectAll() {
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        List<TbSeckillCourse> tbSeckillGoods = redisTemplate.boundHashOps("seckill_list").values();
        if(tbSeckillGoods == null || tbSeckillGoods.isEmpty()){
            QueryWrapper queryWrapper = new QueryWrapper();
//            创建当前时间
            Date date = new Date();
//            开始时间小于或等于  当前时间
            queryWrapper.le("sec_begin_time",date);
//            结束时间大于  当前时间的展示
            queryWrapper.gt("sec_finish_time",date);
//            当前库存数量大于 0
            queryWrapper.gt("sec_numbe",0);
//            进行数据库的查询
            tbSeckillGoods = tbSeckillCourseMapper.selectList(queryWrapper);
//            循环遍历redis查询出来的集合
            for (TbSeckillCourse seckillGood : tbSeckillGoods) {
//                把当前秒杀的商品放置到redis中  string类型   key 秒杀商品ID
                redisTemplate.boundHashOps("seckill_list").put(seckillGood.getSecId(),seckillGood);
                redisTemplate.boundValueOps(seckillGood.getSecId()).set(seckillGood.getSparA());
            }
        }
        return tbSeckillGoods;
    }


    @Override
    public void insertSnapped(Long uid, Long secId) throws Exception {
//        redisTemplate.setValueSerializer(new StringRedisSerializer());
        String o = (String)redisTemplate.boundValueOps(secId).get();
        Long stockCont = Long.parseLong(o);
//        判断当前商品数量是否为零
        if(stockCont <= 0){
//            如果为零直接返回错误
            throw new SeckillExcrption("商品已售罄");
        }

//        如果当前库存自行减一  并返回当前值
        Long increment = redisTemplate.boundValueOps(secId).increment(-1);

        if (increment.intValue() <= 0){
            TbSeckillCourse goods   = (TbSeckillCourse)redisTemplate.boundHashOps("seckill_list").get(secId);
            redisTemplate.boundHashOps("seckill_list").delete(secId);
//          同步数据库
            tbSeckillCourseMapper.updateById(goods);
        }

//        异步生成订单
        Map<String,Object> map = new HashMap<>();
        map.put("uid",uid);
        map.put("secId",secId);
        rocketMQTemplate.convertAndSend("course-topic",map);

    }
}
